<?php
/**公用函数部分*/
namespace App\Http\Controllers;

use App\Http\Models\Shopcart;
use App\Http\Util\UploadFile;
use Illuminate\Support\Facades\DB;

class CommonController extends Controller
{

    public function __construct()
    {
        //
    }

    /**
     * 购物车公用
     * @userid 当前登陆用户id
     * @param['cartlist'] 购物车列表
     * @ param['countnum'] 统计下商品总数
     * @$param['countprice'] 统计商品总价
     */
    public static function shopcart($userid)
    {
        $rent_type['1'] = 'details_nimbleprice';
        $rent_type['2'] = 'details_fixprice';
        $shopcart = new Shopcart();
        $param['cartlist_status'] = $shopcart->lists_status($userid);
        $param['cartlist'] = $shopcart->lists($userid);
        $param['countnum'] = 0;
        $param['countprice'] = 0;
        $param['countbond'] = 0;//押金
        $param['countcoupon'] = 0;//信用额度
        foreach ($param['cartlist_status'] as $v){
            $param['countnum']+=$v->cart_fitment_num;
            $param['countprice']+=$v->cart_fitment_num*$v->$rent_type[$v->cart_rent_type];
            $param['countcoupon'] +=$v->details_bond*$v->cart_fitment_num;
            if($v->cart_rent_type==1){
                $param['countbond']+=$v->cart_fitment_num*$v->details_nimbleprice*2;
            }
        }
        return $param;
    }

    /**生成订单号规则*/
    public static function orderNo()
    {
        return date('YmdHis').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    /**文件上传*/
    public static function upload($key, $path, $fileType,$save_path = '', $extent_path = '')
    {
        $upload = new UploadFile(); // 实例化上传类
        $upload->allowExts = $fileType; //设置附件上传类型
        $upload->maxSize = 1024 * 1000 * 1000 * 20; // 设置附件上传大小20M
        $upload->savePath = $path . $save_path . $extent_path; // 设置附件上传目录
        $upload->uploadReplace = true; //存在同名是否覆盖
        $upload->saveRule = str_pad(rand(000000,999999),'6','0',STR_PAD_LEFT ).date('YmdHis',time()); //文件重命名

        if ($upload->uploadOne($key)) {
            $rs = $upload->getUploadFileInfo();
            $result = array(
                'upload' => true,
                'filename'=> $upload->saveRule.'.'.substr($key['name'], strrpos($key['name'], '.')+1),
                'info' => $rs
            );
        } else {
            $result = array(
                'upload' => false,
                'info' => $upload->getErrorMsg()
            );
        }
        return $result;
    }

    /**
     * 信用额度
     * [用户额度表有额度值时显示，没有给申请信用额度]
     * 【用户id,可用额度,总额度，申请时间，使用额度】
     */
    public static function quota_common($userid=11)
    {
        $res = DB::table('quota')
        ->where('quota_user_id',$userid)
        ->first();
        return $res;
    }

    /**通用颜色图片文件*/
    public static function commmon_color_path()
    {
        $color = [
            '1'=>'/img/color/1.png',
            '2'=>'/img/color/2.png',
            '3'=>'/img/color/3.png',
            '4'=>'/img/color/4.png',
            '5'=>'/img/color/5.png',
            '6'=>'/img/color/6.png',
            '7'=>'/img/color/7.png',
            '8'=>'/img/color/8.png',
            '9'=>'/img/color/9.png'
        ];
        return $color;
    }

}
